package com.formula.datastructure.exercise.queue;

import com.formula.datastructure.util.DataUtil;

public class LinkQueue {
    // front deq, rear enq
    LinkQueueNode front = new LinkQueueNode();
    LinkQueueNode rear = new LinkQueueNode();

    public boolean isEmpty() {
        return front.next == null;
    }

    public void enQueue(Integer data) {
        LinkQueueNode node = new LinkQueueNode();
        node.data = data;
        if (front.next == null) {
            front.next = node;
        } else {
            // 记住这一步, rear.next.next赋值
            rear.next.next = node;
        }
        rear.next = node;
    }

    public Integer deQueue() {
        if (!isEmpty()) {
            LinkQueueNode result = front.next;
            front.next = front.next.next;
            if (front.next == null) {
                rear.next = null;
            }
            return result.data;
        }
        return null;
    }

    public static void main(String[] args) {
        LinkQueue queue = new LinkQueue();
        int size = 20;
        int range = 999;
        Integer[] array = DataUtil.genUniqueArray(size, range);
        DataUtil.printIndex(size);
        DataUtil.printArray(array);
        for (int i = 0; i < size; i++) {
            queue.enQueue(array[i]);
        }

        for (int i = 0; i < size; i++) {
            System.out.println(queue.deQueue());
        }
    }
}
